#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
using namespace std;
int main3751()
{

	int T;
	cin >> T;
	for (int i = 0;i<T;i++)
	{
		string oriStr;
		cin >> oriStr;
		int idx1 = oriStr.find("/");
		int year = atoi(oriStr.substr(0,idx1).c_str());
		int idx2 = oriStr.find("/",idx1 + 1);
		int month = atoi(oriStr.substr(idx1 + 1,idx2 - idx1 -1).c_str());
		int idx3 = oriStr.find("-",idx2 + 1);
		int day = atoi(oriStr.substr(idx2 + 1,idx3 - idx2 - 1).c_str());

		int idx4 = oriStr.find(":",idx3 + 1);
		int hour = atoi(oriStr.substr(idx3 + 1,idx4 - idx3 - 1).c_str());

		int idx5 = oriStr.find(":",idx4 + 1);
		int minute = atoi(oriStr.substr(idx4 + 1,idx5 - idx4 - 1).c_str());

		int second = atoi(oriStr.substr(idx5 + 1).c_str());

		printf("%02d/%02d/%04d",month,day,year);

		if ((hour ==0 && minute == 0 && second == 0) ||
			(hour ==24 && minute == 0 && second == 0))
		{
			printf("-%02d:%02d:%02dam\n",12,0,0);
			continue;
		}

		if (hour == 12 && minute == 0 && second == 0)
		{
			printf("-%02d:%02d:%02dpm\n",12,0,0);
			continue;
		}

		if (hour >=0 && hour < 12)
		{
			if (hour == 0)
			{
				printf("-%02d:%02d:%02dam\n",12,minute,second);
			}else{
				printf("-%02d:%02d:%02dam\n",hour,minute,second);
			}
		}else{
			if (hour == 12)
			{
				printf("-%02d:%02d:%02dpm\n",12,minute,second);			
			}else{
				printf("-%02d:%02d:%02dpm\n",hour - 12,minute,second);			
			}
		}


		
	}
	return 0;
}